const { DataTypes, Model, Deferrable } = require("sequelize");
const sequelize = require("../../sequelize");
/**
 * 规格模型
 */
class Skus extends Model {
    static async addSkus(status, name, order, default1) {
        return await this.create({
            name: name,
            status: status,
            order: order,
            default: default1
        })
    }
    static async updateSkus(status, name, order, default1, id) {
        return await this.update({
            name: name,
            status: status,
            order: order,
            default: default1
        }, {
            where: {
                id: id
            }
        })
    }
    static async updateStatus(status, id) {
        return await this.update({
            status: status
        }, {
            where: {
                id: id
            }
        })
    }
    static async getSkus(page, limit) {
        let row = await this.findAll({
            limit: limit,
            offset: limit * page - limit
        })
        let json = {}
        json["totalCount"] = (await this.count({}))
        json["list"] = []
        row.forEach(value => {
            json.list.push(value.toJSON())
            json.list[json.list.length - 1].status = String(json.list[json.list.length - 1].status)
        })
        return json
    }
    static async deletebyIds(ids) {
        return await this.destroy({
            where: {
                id: ids
            }
        })
    }
};
Skus.init({
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: DataTypes.STRING
    },
    type: {
        type: DataTypes.TINYINT,
        defaultValue: 0
    },
    status: {
        type: DataTypes.TINYINT(1),
        defaultValue: 0
    },
    order: {
        type: DataTypes.INTEGER
    },
    default: {
        type: DataTypes.STRING
    }
}, {
    sequelize,
    modelName: "Skus",
    tableName: "skus",
    createdAt: "create_time",
    updatedAt: "update_time",
})
module.exports = Skus